<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        * {
            margin: 0;
            padding: 0;
        }

        #menu {
            margin: 0 auto;
            width: 100px;
        }

        a {
            display: block;
            text-decoration: none;
            background-color: #ededed;
            color: #005c68;
            font-size: 12px;
            line-height: 30px;
            height: 30px;
            padding-left: 5px;
        }

        a:hover {
            background-color: #005c68;
            color: white;
        }

        /* 折叠状态类 */
        .collapsed {
            height: 25px;
            overflow: hidden;
        }

        .menuSpan {
            display: block;
            height: 25px;
            background-color: #656464;
            color: white;
            font-weight: 700;
            font-size: 14px;
            line-height: 25px;
        }

        /* 伪元素选择器画小三角 */
        .menuSpan::before {
            content: '';
            display: inline-block;
            margin: 0 5px;
            width: 0;
            height: 0;
            border-top: 5px solid transparent;
            border-left: 10px solid white;
            border-bottom: 5px solid transparent;
        }
    </style>
    <script src="tools.js"></script>
    <script>
        window.onload = function () {
            let menuSpan = document.getElementsByClassName("menuSpan");
            let openDiv = menuSpan[0].parentNode;
            for (let i = 0; i < menuSpan.length; i++) {
                menuSpan[i].onclick = function () {
                    
                    // toggleClass(this, "menuSpanAfter");

                    let changeDiv = this.parentNode;
                    toggleMenu(changeDiv, "collapsed");

                    //关闭打开过的菜单
                    if (openDiv != changeDiv && !hasClass(openDiv, "collapsed")) {
                    //  if (openDiv != changeDiv) {
                        /* 
                            addClass(openDiv, "collapsed");
                            为了可以统一处理动画，把addClass替换为toggleClass
                            使用hasClass判断是否执行该功能
                        */
                        // toggleClass(openDiv, "collapsed");
                        toggleMenu(openDiv, "collapsed");
                    }
                    openDiv = changeDiv;
                }
            }
            function toggleMenu(obj){
                let begin = obj.offsetHeight;
                toggleClass(obj, "collapsed");
                let end = obj.offsetHeight;

                obj.style.height = begin + "px";
                move(obj, "height", 10, end, function(){
                    obj.style.height = "";
                });
            }
        }
    </script>
</head>

<body>
    <div id="menu">
        <div class="collapsed">
            <span class="menuSpan">one</span>
            <a href="#">1</a>
            <a href="#">2</a>
            <a href="#">3</a>
            <a href="#">4</a>
            <a href="#">5</a>
            <a href="#">6</a>
        </div>
        <div class="collapsed">
            <span class="menuSpan">two</span>
            <a href="#">1</a>
            <a href="#">2</a>
            <a href="#">3</a>
            <a href="#">4</a>
            <a href="#">5</a>
            <a href="#">6</a>
        </div>
        <div class="collapsed">
            <span class="menuSpan">three</span>
            <a href="#">1</a>
            <a href="#">2</a>
            <a href="#">3</a>
            <a href="#">4</a>
            <a href="#">5</a>
            <a href="#">6</a>
        </div>
    </div>
</body>

</html>